import { _decorator, Component, EventTouch, Label, math, Node, NodeEventType, Size, Sorting2D, UITransform, Vec2, Vec3 } from 'cc';
import EntityBase from '../utils/EntitySystem/EntityBase';
import { CardData } from '../Data/CardData';
import { ProbeEvent, ProbeEventList } from '../utils/EventSystem/EventList';
import CardStateBlock, { CardStateBlock_Ally, CardStateBlock_Enemy, CardStateBlock_Hand } from '../StateBlock/CardStateBlock';
import { ICardDragData } from '../UI/gameUI';
const { ccclass, property } = _decorator;

@ccclass('cardUI')
export class CardUI extends EntityBase {

    private prePos : Vec3;

    public Card : CardStateBlock;

    /**
     * 这张卡位于的位置
     */
    public Index : number;
    
    public set SortOrder(v : number) {
        this.getComponent(Sorting2D).sortingOrder = v;
        this.markedChild.GetComponent<Sorting2D>("cost",Sorting2D.name).sortingOrder = v;
        this.markedChild.GetComponent<Sorting2D>("attack",Sorting2D.name).sortingOrder = v;
        this.markedChild.GetComponent<Sorting2D>("health",Sorting2D.name).sortingOrder = v;
        this.markedChild.GetComponent<Sorting2D>("name",Sorting2D.name).sortingOrder = v;
    }

    
    public get Arrow() : Node {
        return this.markedChild.GetNode("arrow");
    }
    

    /**
     * 卡牌移动结束
     */
    public CardDragEnd : ProbeEventList = new ProbeEventList();

    /**
     * 卡牌移动开始
     */
    public CardDragStart :  ProbeEventList = new ProbeEventList();

    //#region 临时变量
    /**
     * 传出的触摸事件
     */
    public touchEvent : EventTouch
    /**
     * 传入的拖动消息
     */
    public dragInfo : ICardDragData;
    //#endregion

    public InitItem(param: any[]): void {
        super.InitItem(param);
        let card = param[0] as CardStateBlock;
        let parent = param[1] as Node;
        parent.addChild(this.node);

        this.node.on(NodeEventType.TOUCH_MOVE,this._onTouchMove,this);
        this.node.on(NodeEventType.TOUCH_END,this._onTouchEnd,this);
        this.node.on(NodeEventType.TOUCH_START,this._onTouchStart,this);
        this.node.on(NodeEventType.TOUCH_CANCEL,this._onTouchEnd,this);

        this.Card = card;
        this.Card.Start();

        card.Cost.ValueChanged.AddFunc(this._onCostChanged,this);
        card.Health.ValueChanged.AddFunc(this._onHealthChanged,this);
        card.Attack.ValueChanged.AddFunc(this._onAttackChanged,this);

        card.Name.ValueChanged.AddFunc(this._onNameChanged,this);

        card.cardUI = this;
        
        card.setSelf();
        
    }

    public CloseSelf(): void {
        super.CloseSelf();

        this.Card.Cost.ValueChanged.RemoveFunc(this._onCostChanged,this);
        this.Card.Health.ValueChanged.RemoveFunc(this._onHealthChanged,this);
        this.Card.Attack.ValueChanged.RemoveFunc(this._onAttackChanged,this);

        this.Card.Name.ValueChanged.RemoveFunc(this._onNameChanged,this);

        this.Card.Close();
        this.Card = null;

        this.node.off(NodeEventType.TOUCH_MOVE,this._onTouchMove,this);
        this.node.off(NodeEventType.TOUCH_END,this._onTouchEnd,this);
        this.node.off(NodeEventType.TOUCH_START,this._onTouchStart,this);
        this.node.off(NodeEventType.TOUCH_CANCEL,this._onTouchEnd,this);


    }

    public get UITransform() : UITransform
    {
        return this.getComponent(UITransform);
    }

    public returnPos(){
        this.node.setWorldPosition(this.prePos.x,this.prePos.y,0);
        this.prePos = Vec3.ZERO;
    }
    
    

    public onInit(): void {

    }

    public toHand(){
        this.Card.changeState(CardStateBlock_Hand);
    }

    public toAlly(){
        this.Card.changeState(CardStateBlock_Ally);
    }

    public toEnemy(){
        this.Card.changeState(CardStateBlock_Enemy);
    }

    private _onNameChanged(self : CardUI,valueArray : string[])
    {
        let value = valueArray[0];
        self.markedChild.GetComponent<Label>("name",Label.name).string = value;
    }

    private _onCostChanged(self : CardUI,valueArray : number[])
    {
        let value = valueArray[0];
        self.markedChild.GetComponent<Label>("cost",Label.name).string = value.toString();
        if(value > self.Card.CardData.cost)
        {
            self.markedChild.GetComponent<Label>("cost",Label.name).color = math.Color.RED;
        }else if(value < self.Card.CardData.cost)
        {
            self.markedChild.GetComponent<Label>("cost",Label.name).color = math.Color.GREEN;
        }else{
            self.markedChild.GetComponent<Label>("cost",Label.name).color = math.Color.WHITE;
        }
    }

    private _onHealthChanged(self : CardUI,valueArray : number[])
    {
        let value = valueArray[0];
        self.markedChild.GetComponent<Label>("health",Label.name).string = self.Card.Health.Value.toString();
        if(self.Card.IsHurt)
        {
            self.markedChild.GetComponent<Label>("health",Label.name).color = math.Color.RED;
        }else if(self.Card.MaxHealth.Value > self.Card.CardData.health)
        {
            self.markedChild.GetComponent<Label>("health",Label.name).color = math.Color.GREEN;
        }else{
            self.markedChild.GetComponent<Label>("health",Label.name).color = math.Color.WHITE;
        }
    }

    private _onAttackChanged(self : CardUI,valueArray: number[])
    {
        let value = valueArray[0];
        self.markedChild.GetComponent<Label>("attack",Label.name).string = value.toString();
        if(value > self.Card.CardData.attack)
        {
            self.markedChild.GetComponent<Label>("cost",Label.name).color = math.Color.GREEN;
        }else{
            self.markedChild.GetComponent<Label>("cost",Label.name).color = math.Color.WHITE;
        }
    }

    private _onTouchStart(event:TouchEvent) {
        this.prePos = this.node.getWorldPosition();
        this.CardDragStart.DoEvent(this);
    }

    private _onTouchEnd(event : EventTouch) {
        this.SortOrder = 0;
        this.touchEvent = event;
        this.CardDragEnd.DoEvent(this);
        //  传入状态机处理拖动
        this.Card.OnCardDragEnd(this.dragInfo);
        this.touchEvent = null;
        this.dragInfo = null;
    }

    private _onTouchMove(event : EventTouch) {
        this.Card.OnCardDragging(event);
    }

    /**
     * 设置箭头
     * @param r 箭头旋转角度
     * @param des 箭头长度
     */
    public setArrow(r : number,des : number){
        let ui = this.Arrow.getComponent(UITransform);
        this.Arrow.setWorldRotationFromEuler(0,0,r);

        let x = ui.contentSize.x;
        
        ui.setContentSize(new Size(x,des));
    }

}


